/********************************************************************************
Project: "Something Works" in U.S. Jails: Misconduct and Recidivism Effects of the IGNITE Program
File_name: a1_master_QJE.do

Authors: Alsan, Marcella, Arkey M. Barnett, Peter Hull, and Crystal Yang (2025)

Created on:  04/03/2024
Last Modified on: 01/06/2025

Description: This is the master do-file for our project. A replicator that runs 
this program will be able to replicate all figures and tables in the paper, except
for Appendix Table 1, which is created using R.

Note: We have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

********************************************************************************
// (0) SETUP
********************************************************************************

* Open log file
		cap log close
		local logdate = string( d(`c(current_date)'), "%dDDmonCCYY" )
		global logs "C:/replication_ignite/4logs"
		capture mkdir "$logs"
		log using "$logs/replication_clean_`logdate'.txt", text replace
*Set conditions
		clear all
		set more off
		set linesize 200
		set maxvar 32767
		set scheme burd4
		graph set window fontface "Times New Roman"
		set seed 42
		set sortseed 13
		global date = "final"
* Set path directories
		local currentuser = c(username)
	//These folders should not be changed
		global drive "C:/replication_ignite"
		global code "$drive/1code_QJE"
		global input_data "$drive/2input_data"
	//These folders should be changed or not already existing
		global output_data "$drive/3output_data"
		global figures "$drive/5figures"
		global tables "$drive/6tables"
		macro list _all

********************************************************************************
// (1) INSTALL PACKAGES NEEDED FOR REPLICATION 
********************************************************************************

*Packages needed
		ssc inst missings, replace
		capture net install gr0075.pkg
		capture ssc uninstall ftools
		capture ado uninstall ftools
		net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")
		capture ado uninstall reghdfe
		capture ssc uninstall reghdfe
		net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")
		capture ado uninstall ivreg2
		ssc install ivreg2, replace
		cap ado uninstall ivreghdfe
		net install ivreghdfe, from("https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/") replace
		ssc install unique, replace
		ssc install gtools, replace
		gtools, upgrade
		ssc install binscatter, replace
		ssc install scheme_tufte, replace
		ssc install palettes, replace
		ssc install colrspace, replace
		ssc install schemepack, replace
		capture net install dm0090.pkg

********************************************************************************
// (2) Set Global Macro Controls 
********************************************************************************
		
* set controls to be used for main analysis. Key variables
	//Design Controls
		global design_control mis_dui mis_traffic mis_crime ///
			 month_booked dow_scheduled cat_num_charge cat_division
	//Auxiliary Controls		
		global auxiliary_control D_female age_17_24 age_25_34 age_35_44 ///
			age_45_54 age_55_64  D_Black prior_offense_1y D_public_defender ///
			D_missing pctebll pc_hs log_med_hhincome pc_black 

********************************************************************************
// (3) Generate Output Folders for Replication
********************************************************************************
			 
*Create folder Needed 
		mkdir "$output_data"
		mkdir "$output_data/D67"
		mkdir "$figures"
		mkdir "$tables"
		mkdir "$output_data/covid_data"
		
********************************************************************************
// (4) Clean data for Replication
********************************************************************************
		
*************************
**CLEAN Census Tract Characteristics
*************************
		do "$code/b0_zipcode_variables_QJE.do"

*************************
**CLEAN Genesee County Seperately
*************************
		do "$code/b1_clean_district_ROA_QJE.do"
		do "$code/b2_merge_JMS_QJE.do" //Contains PII (full name): Do not send
		do "$code/b3_incident_JMS_merged_QJE.do"
		do "$code/b4_district_ROA_merged_QJE.do" //Contains PII (case numbers): Do not send
		do "$code/b5_make_estimate_sample_QJE.do"
		
*************************
**CLEAN Saginaw County Seperately
*************************
		do "$code/b6_1_bookingrecords_saginaw_clean_QJE.do" //Contains PII (booking numbers): Do not send
		do "$code/b6_2_bookingrecords_saginaw_work_QJE.do" //Contains PII (booking numbers): Do not send
		do "$code/b7_0_casenumbers_saginaw_QJE.do"
		do "$code/b7_1_sag_roa_precrime_QJE.do"
		do "$code/b7_2_sag_roa_postcrime_QJE.do"
		do "$code/b7_3_casenumbers_saginaw_repull_QJE.do"
		do "$code/b7_4_sag_roa_crim_repulled_QJE.do"
		do "$code/b8_1_sag_estimate_sample_QJE.do"
		
*************************
**Combine Genesee and Saginaw County
*************************
		do "$code/b8_2_sag_gen_roa_analysis_QJE.do"

*************************
**Clean Survey Data
*************************
		do "$code/b9_fcs_cleaning_QJE.do"
		do "$code/b10_cfs_cleaning_QJE.do"

*************************
**Clean Robustness Check/Additional Outcomes Data
*************************
		do "$code/b11_1_alternative_delays_QJE.do"		
		do "$code/b11_2_probation_violation_QJE.do"
		
*************************
** Clean Mt. Morris Schools
*************************
		do "$code/b12_clean_mt_morris_educ_QJE.do"
			
*************************
** Clean Kites Data
*************************
		do "$code/b13_kites_cleaning_QJE.do"
			
********************************************************************************
// (5) GENERATE TABLES AND FIGURES IN THE MAIN PAPER 
********************************************************************************
					
capture log close
local logdate = string( d(`c(current_date)'), "%dDDmonCCYY" )
log using "$logs/main_QJE_replication_analysis_`logdate'.txt", text replace
					//C: Tables (5 programs)
						do "$code/c1_balance_robust_QJE.do"
						do "$code/c2_complier_characteristics_QJE.do"
						do "$code/c3_main_iv_QJE.do" 
						do "$code/c4_robustness_checks_QJE.do"
						do "$code/c5_sag_gen_estimate_QJE.do" 
					//D: Figures (4 programs)
						do "$code/d1_obs_pred_rec_QJE.do"
						do "$code/d2_reduced_form_binscatter_QJE.do"
						do "$code/d3_recidivism_misconduct_by_time_QJE.do"
						do "$code/d4_mt_morris_educ_score_lvl_QJE.do"
	

	
********************************************************************************
// (6) GENERATE TABLES AND FIGURES IN THE ONLINE APPENDIX 
********************************************************************************
	
capture log close
local logdate = string( d(`c(current_date)'), "%dDDmonCCYY" )
log using "$logs/appendix_QJE_replication_analysis_`logdate'.txt", text replace		
					//E: Appendx Figures (17 Programs)
						do  "$code/e1_arrests_by_living_address_QJE.do"
						*****Appendix Figure I produced by using output from above into "$code/e1_arrest_maps_GC.R"
						do  "$code/e4_gcso_budget_QJE.do"
						do  "$code/e11_chromebook_descriptive_QJE.do"
						do  "$code/e12_acr_weights_QJE.do"
						do  "$code/e13_fs_binscatter_QJE.do"
						do  "$code/e14_demo_het_QJE.do"
						do  "$code/e15_predict_risk_het_QJE.do" 
						do  "$code/e16_predict_time_in_jail_het_QJE.do"
						do  "$code/e17_covid_cases_QJE.do"
						do  "$code/e18_peopleinjail_daily_QJE.do"
						do  "$code/e19_kites_eventstudy_courtclosure_QJE.do"
						do  "$code/e20_crime_figures_QJE.do"
						do  "$code/e21_sag_gen_eventstudy_QJE.do"
						do  "$code/e22_prob_ignite_ym_booked_QJE.do"
						do  "$code/e23_tornado_plots_QJE.do"
						do  "$code/e24_cfs_pos_neg_neutral_QJE.do"
						do  "$code/e25_kites_sentiment_analysis_QJE.do"
	

					//F: Appendix Tables (14 Programs)
						do "$code/f1_mt_morris_test_taker_representative_QJE.do"
						do "$code/f2_community_survey_balance_QJE.do"
						do "$code/f3_corrections_survey_balance_QJE.do"
						do "$code/f4_summary_stats_QJE.do"
						do "$code/f5_balance_preignite_wpred_QJE.do"
						do "$code/f6_balance_postignite_wpred_QJE.do"	
						do "$code/f7_attrition_QJE.do"
						do "$code/f8_first_stage_QJE.do"
						do "$code/f9_secondary_alternatives_QJE.do"
						do "$code/f11_sag_gen_diffindiff_QJE.do"
						do "$code/f12_cost_benefit_QJE.do"
						do "$code/f13_fcs_3outcomes_QJE.do"
						do "$code/f14_cfs_effects_work_inmate_QJE.do"
						do "$code/f15_cfs_effects_work_exp_QJE.do"
	
capture log close	